package Server.Add;

import bean.novelBean;
import dao.novelDao;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;

public class novelServer {
    public String insert (HttpServletRequest request) {

        String novelNumber = "0";
        String msg = "";

        try {
            String novelName,novelImage, novelIntroduce;

            novelName  	= request.getParameter("novelName");
            novelIntroduce  	= request.getParameter("novelIntroduce");

            if (novelName == null || novelIntroduce == null) {
                msg = "输入不正确！";										//值为null的原因：输入框名称不对，或者在地址栏直接输入本页网址
                return novelNumber;
            }
            novelName 	= novelName.trim();
            novelIntroduce 		= novelIntroduce.trim();

            if (novelName.equals("")) {
                msg += "请输入商品名称。";
                return novelNumber;
            }

            if (novelName.length() > 45) {
                msg += "商品名称的字符数不能超过45。";
                return novelNumber;
            }


            String goodsIdUUID = request.getParameter("goodsIdUUID");		//从隐藏域控件中获取值

            if (goodsIdUUID == null || goodsIdUUID.equals("")) {
                msg += "获取唯一标识发生错误。";
                return novelNumber;
            }

            novelImage = request.getSession().getAttribute("novelImage" + goodsIdUUID).toString();	//从session中获得图片名


            novelBean novel = new novelBean();

            //novel.setGoodsId(novelNumber);
            novel.setNovelName(novelName);
            novel.setNovelImage(novelImage);
            novel.setNovelIntroduce(novelIntroduce);

            novelDao novelDao = new novelDao();
            novelNumber = novelDao.insert(request,novel);			//****新添记录

            if (novelNumber.equals("0")) {
                msg += "新添记录失败！请重试。";
                return novelNumber;
            }

            //------------------------ 将图片用新的goodsId重命名，更新数据表中字段image的值
            String path = request.getServletContext().getRealPath(""); 	//当前项目运行时所在的物理目录
            String uploadFolder = "novelImage/";						//存放文件的子目录
            path += uploadFolder; 													//文件存放的目录

            File fileOld = new File(path, novelImage); 									//创建File对象

            if (fileOld.exists() == false || fileOld.isFile() == false) {			//如果不存在，或者不是文件
                msg = "上传的文件已不存在。";
                return novelNumber;
            }

            int index = novelImage.lastIndexOf(".");					//路径中最后一个“.”的位置
            String nameExt = novelImage.substring(index + 1);		//获取文件的扩展名
            novelImage = novelNumber + "." + nameExt;
            File fileNew = new File(path, novelImage); 				//创建File对象

            if (fileNew.exists()) {								//如果存在
                if (fileNew.delete() == false) {				//如果删除失败
                    msg = "删除旧文件失败。";
                    return novelNumber;
                }
            }

            if (fileOld.renameTo(fileNew) == false) {			//如果图片重命名失败
                msg = "文件重命名失败。";
                return novelNumber;
            }

            int count = novelDao.update(request, novelNumber, novelImage);		//****更新记录。目的是根据goodsId更新image

            if (count == 0) {
                msg += "数据表中的商品图片名称更新失败！";
                return novelNumber;
            }

            msg += "新添记录成功。";
            request.getSession().setAttribute("msg", msg);		//通过session传递消息。消息将显示在详情页中

            //最后调用分解器

             path = request.getServletContext().getRealPath("")+"novelTXT";
            util.Resolver.resolver(path);

            return novelNumber;										//返回新添记录的ID

        } catch (Exception e) {
            msg += "系统发生错误。";
            e.printStackTrace();
        } finally {
            if (request.getAttribute("msg") != null) {
                msg += request.getAttribute("msg").toString();
            }
            request.setAttribute("msg", msg);
        }

        return novelNumber;
    }

    public boolean update (HttpServletRequest request) {

        String msg = "";

        try {
            String novelNumber = request.getParameter("novelNumber"); 			//获取地址栏参数id的值

            try {
                Integer.parseInt(novelNumber);
            } catch (Exception e) {
                msg += "参数novelNumber错误！";
                return false;
            }

            String  novelName,  novelIntroduce;

            novelName  	= request.getParameter("novelName");
            novelIntroduce  	= request.getParameter("novelIntroduce");

            if (novelName == null ) {
                msg = "输入不正确！";										//值为null的原因：输入框名称不对，或者在地址栏直接输入本页网址
                return false;
            }
            novelName 	= novelName.trim();
            novelIntroduce 		= novelIntroduce.trim();

            if (novelName.equals("")) {
                msg += "请输入商品名称。";
                return false;
            }

            if (novelName.length() > 45) {
                msg += "商品名称的字符数不能超过45。";
                return false;
            }

            novelBean novel = new novelBean();

            novel.setNovelNumber(novelNumber);
            novel.setNovelName(novelName);
            novel.setNovelIntroduce(novelIntroduce);

            novelDao novelDao = new novelDao();
            int count = novelDao.update(request, novel);		//****更新记录

            if (count == 0) {
                msg += "商品信息修改失败！请重试。";
                return false;
            }

            msg += "商品信息修改成功！";
            request.getSession().setAttribute("msg", msg);		//通过session传递消息。消息将显示在详情页中
            return true;

        } catch (Exception e) {
            msg += "系统发生错误。";
            e.printStackTrace();
        } finally {
            if (request.getAttribute("msg") != null) {
                msg += request.getAttribute("msg").toString();
            }
            request.setAttribute("msg", msg);
        }

        return false;
    }



}
